package com.axend.aerosense.user.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.services.ILoginService;
import com.axend.aerosense.common.services.config.ServicesConfig;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.user.R;
import com.axend.aerosense.user.UserSettingActivity;
import com.axend.aerosense.user.databinding.UserSettingStep1FragmentBinding;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushManager;

import io.reactivex.disposables.Disposable;

public class UserSettingStep1Fragment extends MvvmBaseFragment<UserSettingStep1FragmentBinding, ViewModel> {
    @Autowired(name = ServicesConfig.User.LONGING_SERVICE)
    ILoginService iLoginService;
    private Disposable disposable;

    public static UserSettingStep1Fragment getInstance() {
        UserSettingStep1Fragment fragment = new UserSettingStep1Fragment();
//        Bundle bundle = new Bundle();
//        bundle.putString(ARG_PARAM, str);
//        fragment.setArguments(bundle);   //设置参数
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        ((UserSettingActivity) getActivity()).setTitle(getString(R.string.user_setting));
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        viewDataBinding.userAccountSafeTitle.setOnClickListener(v -> {
            changeFragment(UserSettingStep2Fragment.getInstance());
        });

        viewDataBinding.userHelpTitle.setOnClickListener(v -> {
            changeFragment(UserAboutFragment.getInstance());
        });

        viewDataBinding.userLogoutTitle.setOnClickListener(v -> {
            MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.NOTICE, "");
            iLoginService.logout(getActivity(), () -> {
                iLoginService.login();
                //解除tpns推送绑定
                XGPushManager.unregisterPush(getContext(), new XGIOperateCallback() {
                    @Override
                    public void onSuccess(Object o, int i) {
                        //解除成功
                    }

                    @Override
                    public void onFail(Object o, int i, String s) {
                        //解除失败
                    }
                });
                getActivity().finish();
            });
        });

        viewDataBinding.userProtectPersonView.setOnClickListener(v -> {
            Fragment fragment = UserSetNoticeFragment.getInstance();
            FragmentUtil.add(getActivity(), ((UserSettingActivity) getActivity()).getContentId(), fragment, this, true);
        });
    }

    private void changeFragment(Fragment fragment) {
        FragmentUtil.add(getActivity(),
                ((UserSettingActivity) getActivity()).getContentId(),
                fragment,
                this,
                true);
    }


    @Override
    public int getLayoutId() {
        return R.layout.user_setting_step1_fragment;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected ViewModel getViewModel() {
        return null;
    }

    @Override
    protected void onRetryBtnClick() {

    }
}
